using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GMap.NET;
using GMap.NET.WindowsForms;
using GMap.NET.MapProviders;
using GMap.NET.WindowsForms.Markers;
using GMap.NET.Projections;

namespace Comteck.Winforms.Controls {
  /// <summary>
  /// 扩展高德地图provider  add by alex 2019-07-19
  /// </summary>
  public class AMapProvider : AMapProviderBase {
    public static readonly AMapProvider Instance;

    readonly Guid id = Guid.NewGuid();
    public override Guid Id => id;
    //高德
    readonly string name = "AMap";
    public override string Name => name;

    static AMapProvider() {
      Instance = new AMapProvider();
    }
    /// <summary>
    /// 根据URL获取图片
    /// </summary>
    /// <param name="pos">鼠标信息</param>
    /// <param name="zoom">缩放比</param>
    /// <returns></returns>
    public override PureImage GetTileImage(GPoint pos, int zoom) {
      try {
        string url = MakeTileImageUrl(pos, zoom, LanguageStr);
        return GetTileImageUsingHttp(url);
      } catch {
        return null;
      }
    }
    /// <summary>
    /// 根据高德地图加载原理获取url
    /// </summary>
    /// <param name="pos"></param>
    /// <param name="zoom"></param>
    /// <param name="language"></param>
    /// <returns></returns>
    string MakeTileImageUrl(GPoint pos, int zoom, string language) {
      var num = (pos.X + pos.Y) % 4 + 1;
      string url = string.Format(UrlFormat, pos.X, pos.Y, zoom);
      return url;
    }
    static readonly string UrlFormat = "http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={0}&y={1}&z={2}";
  }
}
